package com.hwg.shiliupro.controller;
import com.hwg.shiliupro.annotation.Authority;
import com.hwg.shiliupro.finals.Code;
import com.hwg.shiliupro.po.HomePage;
import com.hwg.shiliupro.po.R;
import com.hwg.shiliupro.service.HomePageService;
import com.hwg.shiliupro.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/homePages")
public class HomePageController {
    @Autowired
    private HomePageService homePageService;

    /**
     * 获取所有待审核的个人主页信息
     *
     * @return {@link R}
     */
    @GetMapping
    @Authority("homePage:getAllWaitCheck")
    public R getAllWaitCheck(){
        List<HomePage> all = homePageService.getAll();
        return R.ok().code(Code.GET_OK).data(all);
    }

    /**
     * 管理员审核信息  flag标识 通过还是不通过  1通过  0 不通过
     *
     * @return {@link R}
     */
    @PutMapping("/check/{flag}")
    @Authority("homePage:check")
    public R checkInfo(@RequestBody HomePage homePage,@PathVariable Integer flag){
        log.info("接收到的参数：{}和标识{}",homePage,flag);
        if(homePageService.checkInfo(homePage, flag)){
            return R.ok().code(Code.UPDATE_OK);
        }
        return R.error().code(Code.UPDATE_ERR).message("审核失败，请稍后再试");
    }

    /**
     * 获取个人主页信息 通过id
     * @param id
     * @return {@link R}
     */
    @GetMapping("/{id}")
    @Authority("homePage:getOne")
    public R getInfoById(@PathVariable Integer id){
        HomePage infoById = homePageService.getInfoById(id);
        if(infoById!=null){
            return R.ok().code(Code.GET_OK).data(infoById);
        }
        return R.error().code(Code.GET_ERR).message("未获取到个人主页信息");
    }

    /**
     * 用户提交个人主页信息 供审核
     *
     * @param homePage 主页
     * @return {@link R}
     */
    @PostMapping
    @Authority("homePage:sumbitInfo")
    public R sumbitInfo(@RequestBody HomePage homePage){
        if(homePageService.sumbitInfo(homePage)){
            return R.ok().code(Code.UPDATE_OK);
        }
        return R.error().code(Code.UPDATE_ERR).message("提交审核失败,请稍后再试~");
    }

    /**
     * 返回用户头像base64
     *
     * @param request 请求
     * @return {@link R}
     */
    @GetMapping("/img")
    @Authority("homePage:returnImg")
    public R getImg(HttpServletRequest request){
        Map userByJwtToken = JwtUtils.getUserByJwtToken(request);
        Integer userId = (Integer) userByJwtToken.get("id");
        String base64 = homePageService.getImgByUserId(userId);
        if(base64!=null){
            return R.ok().code(Code.GET_OK).data(base64);
        }
        return R.error().code(Code.GET_ERR).message("该用户未存储过头像~");
    }

    /**
     * 保存用户头像
     *
     * @param base64  base64
     * @param request 请求
     * @return {@link R}
     */
    @PostMapping("/img")
    @Authority("homePage:saveImg")
    public R saveImg(@RequestBody String base64, HttpServletRequest request){
        Map userByJwtToken = JwtUtils.getUserByJwtToken(request);
        Integer userId = (Integer) userByJwtToken.get("id");
        if(homePageService.saveImg(base64,userId)){
            return R.ok().code(Code.SAVE_OK);
        }
        return R.error().code(Code.SAVE_ERR).message("头像保存失败~");
    }
}
